package com.gxsyh.nantie.phone.provider.service;
import java.util.Date;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxsyh.nantie.commons.dto.PageHelpDTO;
import com.gxsyh.nantie.commons.dto.PageHelpPhoneDTO;
import com.gxsyh.nantie.phone.provider.api.domain.SelectDiscuss;
import com.gxsyh.nantie.phone.provider.api.domain.TbDiscuss;

import javax.annotation.Resource;

import com.gxsyh.nantie.phone.provider.api.domain.TbInformation;
import com.gxsyh.nantie.phone.provider.mapper.SelectDiscussMapper;
import com.gxsyh.nantie.phone.provider.mapper.TbDiscussMapper;
import com.gxsyh.nantie.phone.provider.api.TbDiscussService;
import org.apache.dubbo.config.annotation.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @email yom535@outlook.com
 * @author: 刘锋嘉(Liu Fengjia)
 * @date: 2020/2/17
 * @time: 10:08
 * @Description: 
 */
@Service(version = "1.0.0")
public class TbDiscussServiceImpl implements TbDiscussService{

    @Resource
    private TbDiscussMapper tbDiscussMapper;

    @Resource
    private SelectDiscussMapper selectDiscussMapper;

    @Override
    public int addDiscuss(Long id,String content,Long userId) {
        TbDiscuss tbDiscuss=new TbDiscuss();
        tbDiscuss.setId(id);
        tbDiscuss.setContent(content);
        tbDiscuss.setCreatetime(new Date());
        tbDiscuss.setUserId(userId);
        return tbDiscussMapper.insert(tbDiscuss);
    }

    @Override
    public PageHelpPhoneDTO selectAllDiscuss(int pageNum) {
        PageHelper.startPage(pageNum,5,"createTime DESC");
        PageInfo<SelectDiscuss> pageInfo=new PageInfo<>(selectDiscussMapper.selectAllDiscuss());
        List<SelectDiscuss> allDiscuss = pageInfo.getList();
        PageHelpPhoneDTO<SelectDiscuss> pageHelpDTO=new PageHelpPhoneDTO();
        pageHelpDTO.setPages(pageInfo.getPages());
        pageHelpDTO.setTotal(pageInfo.getTotal());
        pageHelpDTO.setNextPage(pageInfo.getNextPage());
        pageHelpDTO.setData(allDiscuss);
        return pageHelpDTO;
    }

    @Override
    public int deleteDiscuss(Long discussId) {
        Example example=new Example(TbDiscuss.class);
        example.createCriteria().andEqualTo("id",discussId);
        return tbDiscussMapper.deleteByExample(example);
    }

    @Override
    public TbDiscuss selectOneDiscuss(Long discussId) {
        Example example=new Example(TbDiscuss.class);
        example.createCriteria().andEqualTo("id",discussId);
        return tbDiscussMapper.selectOneByExample(example);
    }


}
